package com.ctshk.rpc.order.tour.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ctshk.common.enums.NumberConstant;
import com.ctshk.rpc.order.tour.code.CollectionRefundWayIdCode;
import com.ctshk.rpc.order.tour.code.IsDeletedCode;
import com.ctshk.rpc.order.tour.code.SeriesOrderTouristOtherFeeFeeTypeCode;
import com.ctshk.rpc.order.tour.req.SeriesOrderTouristInfoListCollectionReq;
import com.ctshk.rpc.order.tour.req.SeriesOrderTouristInfoListCollectionTripReq;
import com.ctshk.rpc.order.tour.req.SeriesOrderTouristInfoListReq;
import com.ctshk.rpc.order.tour.req.custom.CustomOrderIdReq;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

/**
 * <p>
 * 系列团订单 Provider 接口
 * </p>
 *
 * @author 谭响
 * @since 2021-01-18
 */
public class SeriesOrderTouristInfoProvider {

    public String selectListBySelective(IPage<SeriesOrderTouristInfoListReq> page, @Param("req") SeriesOrderTouristInfoListReq req) {
        return new SQL() {{
            SELECT("tsoti.id, tsoti.order_id, tsoti.main_data_id, tsoti.tourist_type_name, tsoti.birthday, tsoti.mobile_area_code, tsoti.mobile, tsoti.email, " +
                    "tsoti.cn_surname, tsoti.cn_name, tsoti.en_surname, tsoti.en_name,tsoti.is_full_info, " +
                    "tsoti.certificate_type1, tsoti.certificate_number1, tsoti.certificate_valid_date1, tsoti.certificate_place1, tsoti.remark, " +
                    "tsoti.emergency_contact_name, tsoti.emergency_mobile_area_code, tsoti.emergency_mobile, tsoti.pay_status, tsotq.refund_status AS quit_refund_status, " +
                    "tsoti.total_receivables, tsoti.total_paid_in, tsoti.remaining_receivables ");
            FROM("t_series_order_tourist_info tsoti");
            LEFT_OUTER_JOIN("( SELECT temp.tourist_id, temp.refund_status,temp.is_deleted FROM t_series_order_tourist_quit temp ORDER BY temp.gmt_create DESC LIMIT 1 ) tsotq ON tsotq.tourist_id = tsoti.id");
            WHERE("tsoti.order_id=#{req.orderId}");
            WHERE("tsoti.is_deleted=" + IsDeletedCode.NO.getCode());
            WHERE("(tsotq.is_deleted=0 or tsotq.is_deleted IS NULL)");
            WHERE("(tsotq.refund_status NOT IN(3) OR tsotq.refund_status IS NULL)");
            ORDER_BY("tsoti.main_data_id = 1353957574038048769 desc");
        }}.toString();
    }

    public String selectCalculationBySelective(List<Long> touristIdList) {
        StringBuilder sb = new StringBuilder();
        sb.append("select tsoti.id, tsoti.order_id, tsoti.main_data_id, tsoti.tourist_type_name, " +
                "tsoti.cn_surname, tsoti.cn_name, tsoti.en_surname, tsoti.en_name, " +
                "total_receivables " +
                "from t_series_order_tourist_info tsoti ");
        sb.append("where 1=1 ");
        if (touristIdList.size() > 0) {
            sb.append(" and tsoti.id in ( ");
            StringBuilder temp = new StringBuilder();
            int i = 0;
            for (Long id : touristIdList) {
                i++;
                temp.append("#{touristIdList[" + (i - 1) + "]}");
                if (i < touristIdList.size()) {
                    temp.append(",");
                }
            }
            sb.append(temp);
            sb.append(" ) ");
        }
        return sb.toString();
    }

    public String selectListCollectionBySelective(IPage<CustomOrderIdReq> page, @Param("req") CustomOrderIdReq req) {
        StringBuilder sb = new StringBuilder();
        sb.append("select tsoti.id, tsoti.order_id, tsoti.main_data_id, tsoti.tourist_type_name,tsoti.collection_fee_json, ");
        sb.append("tsoti.cn_surname, tsoti.cn_name, tsoti.en_surname, tsoti.en_name, ");
        sb.append("tsoti.group_fee, tsoti.pay_status, tsotq.refund_status AS quit_refund_status, ");
        sb.append("(SELECT group_concat( CONCAT( a.amount, '])', a.plus_type, '])', a.NAME ) SEPARATOR '})' ) FROM t_series_order_tourist_fee_adjust a WHERE a.tourist_id = tsoti.id AND a.order_id = #{req.id} ) feeAdjustDTOList, ");
        sb.append("(SELECT group_concat( CONCAT( a.amount, '])', a.fee_name) SEPARATOR '})' ) FROM t_series_order_tourist_other_fee a WHERE a.tourist_id = tsoti.id AND a.order_id = #{req.id} AND a.fee_type = " + NumberConstant.ONE.getValue() +" ) collectionFeeDTOList, ");
        sb.append("(SELECT group_concat( CONCAT( a.amount, '])', a.fee_name) SEPARATOR '})' ) FROM t_series_order_tourist_other_fee a WHERE a.tourist_id = tsoti.id AND a.order_id = #{req.id} AND a.fee_type = "+ NumberConstant.TWO.getValue() +" ) otherFeeDTOList, ");
        sb.append("tsoti.total_receivables, tsoti.total_paid_in,tsoti.remaining_receivables ");
        sb.append("from t_series_order_tourist_info tsoti ");
        sb.append("left join ( SELECT temp.tourist_id, temp.refund_status FROM t_series_order_tourist_quit temp ORDER BY temp.gmt_create DESC LIMIT 1 ) tsotq ON tsotq.tourist_id = tsoti.id ");
        sb.append("where tsoti.order_id=#{req.id} AND tsoti.is_full_info = 1 AND tsoti.is_deleted = 0");
        return sb.toString();
    }

    public String selectListCollectionTripBySelective(IPage<SeriesOrderTouristInfoListCollectionTripReq> page, @Param("req") SeriesOrderTouristInfoListCollectionTripReq req) {
        StringBuilder sb = new StringBuilder();
        sb.append("select tsoti.id, tsoti.order_id, tsoti.main_data_id, tsoti.tourist_type_name, ");
        sb.append("tsoti.cn_surname, tsoti.cn_name, tsoti.en_surname, tsoti.en_name, ");
        sb.append("tsoti.group_fee, tsoti.pay_status, tsotq.refund_status AS quit_refund_status, ");
        sb.append("tsoti.collection_fee, tsoti.adjustment_cost, tsoti.other_amount, ");
        sb.append("tsoti.total_receivables, tsoti.total_paid_in, tsoti.remaining_receivables, ");
        sb.append("tso.order_number ");
        sb.append("from t_series_order_tourist_info tsoti ");
        sb.append("left join t_series_order tso on tsoti.order_id = tso.id ");
        sb.append("left join ( SELECT temp.tourist_id, temp.refund_status FROM t_series_order_tourist_quit temp ORDER BY temp.gmt_create DESC LIMIT 1 ) tsotq ON tsotq.tourist_id = tsoti.id ");
        sb.append("where tsoti.schedule_id=#{req.scheduleId} ");
        sb.append("and tsoti.is_deleted=" + IsDeletedCode.NO.getCode() + " ");
        return sb.toString();
    }
}
